name: Ariane scheduled workflows

on:
  # Run every 6 hours
  schedule:
    - cron: '0 */6 * * *'

permissions:
  # To be able to access the repository with actions/checkout
  contents: read
  # To trigger workflows via workflow_dispatch
  actions: write

jobs:
  ariane-scheduled:
    name: Run Scheduled Workflows
    strategy:
      fail-fast: false
      matrix:
        branch:
          - "1.12"
          - "1.13"
          - "1.14"
          - "1.15"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout branch
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          ref: v${{ matrix.branch }}
          persist-credentials: false

      - name: Manually run Ariane workflows from the branch
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          BRANCH="${{ matrix.branch }}"
          REF="v${BRANCH}"
          SHA=$(git rev-parse ${REF})
          readarray workflows < <(TRIGGER="/test-backport-${BRANCH}" yq '.triggers[env(TRIGGER)].workflows[]' .github/ariane-config.yaml)

          for workflow in ${workflows[@]}; do
            echo triggering ${workflow}
            gh workflow run ${workflow} \
              --ref ${REF} \
              -f PR-number=${REF/./-} \
              -f context-ref=${REF} \
              -f SHA=${SHA}
          done
